<?php
declare (strict_types = 1);
namespace app\common\logic\navi;

use app\common\model\navi\NaviTop as NaviTopModel;
use app\common\exception\LogicException;

/**
 * 头部导航配置逻辑层
 */
class NaviTop extends NaviTopModel
{

    public function getFetchInfo($id)
    {
        return $this
	        ->field('id,pid,title,content,data_type')
	        ->findOrEmpty($id);
    }

    public function getContent($id)
    {
    	return $this->where('id', $id)->value('content');
    }

    public function getTree($pid = 0)
    {
        $field = 'id,pid,title,content,data_type';
        $navi = $this->field($field)->where('pid', $pid)->where('mhm_id', request()->mhm_id)->order(['sort','update_time'=>'desc'])->select();
        if ($navi) {
            foreach ($navi as $n) {
                if ( $this->hasChildren($n->id) ) {
                    $n->child = $this->getTree($n->id);
                }
            }
        }
        return $navi;
    }

    // 是否有子集
    public function hasChildren(int $pid = 0): bool
    {
        return $this->where('pid', $pid)->find() ? true : false;
    }
}
